//
//  LBDiscussBidMessageCell.m
//  app
//
//  Created by 刘文扬 on 16/8/22.
//  Copyright © 2016年 广东联结电子商务有限公司. All rights reserved.
//

#import "LBDiscussBidMessageCell.h"
#import "LBDiscussBidMessage.h"
#import <MDHTMLLabel.h>
@interface LBDiscussBidMessageCell()


@end



@implementation LBDiscussBidMessageCell

+ (CGSize)sizeForMessageModel:(RCMessageModel *)model
      withCollectionViewWidth:(CGFloat)collectionViewWidth
         referenceExtraHeight:(CGFloat)extraHeight {
    LBDiscussBidMessage *message = (LBDiscussBidMessage *)model.content;
    CGSize size = [LBDiscussBidMessageCell getBubbleBackgroundViewSize:message];
    
    CGFloat __messagecontentview_height = size.height;
    __messagecontentview_height += (extraHeight - 40);
    
    return CGSizeMake(collectionViewWidth, __messagecontentview_height);
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize {
    self.bubbleBackgroundView = [[UIImageView alloc] initWithFrame:CGRectZero];
    [self.messageContentView addSubview:self.bubbleBackgroundView];
    
    self.textLabel = [[MDHTMLLabel alloc] initWithFrame:CGRectZero];
    self.textLabel.numberOfLines = 0;
    self.textLabel.font = [UIFont systemFontOfSize:15];
    [self.bubbleBackgroundView addSubview:self.textLabel];
    self.bubbleBackgroundView.userInteractionEnabled = YES;
    
    [self.contentView addSubview:self.headerView];
    
}

-(void)setDataModel:(RCMessageModel *)model
{

    model.isDisplayMessageTime = NO;
    [super setDataModel:model];
    self.nicknameLabel.text = @"客服小蜜";
    [self setAutoLayout];
    
}

- (void)setAutoLayout {
    LBDiscussBidMessage *discussMessage = (LBDiscussBidMessage *)self.model.content;
    if (discussMessage) {
 

        NSString *htmlString = discussMessage.content;
        self.textLabel.htmlText = htmlString;
    }
    
    CGSize textLabelSize = [[self class] getTextLabelSize:discussMessage];
    CGSize bubbleBackgroundViewSize = [[self class] getBubbleSize:textLabelSize];
    CGRect messageContentViewRect = self.messageContentView.frame;
    
    //拉伸图片
    if (MessageDirection_RECEIVE == self.model.messageDirection)
    {
        self.textLabel.frame = CGRectMake(20, 5, textLabelSize.width, textLabelSize.height);
        
        messageContentViewRect.size.width = bubbleBackgroundViewSize.width;
        self.messageContentView.frame = messageContentViewRect;
        
        self.bubbleBackgroundView.frame = CGRectMake(-8, 0, bubbleBackgroundViewSize.width, bubbleBackgroundViewSize.height);
        UIImage *image = [RCKitUtility imageNamed:@"chat_from_bg_normal" ofBundle:@"RongCloud.bundle"];
        self.bubbleBackgroundView.image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(image.size.height * 0.8, image.size.width * 0.8, image.size.height * 0.2, image.size.width * 0.2)];
    }
    else
    {
        self.textLabel.frame = CGRectMake(12, 5, textLabelSize.width, textLabelSize.height);
        
        messageContentViewRect.size.width = bubbleBackgroundViewSize.width;
        messageContentViewRect.origin.x = self.baseContentView.bounds.size.width - (messageContentViewRect.size.width + 12 + [RCIM sharedRCIM].globalMessagePortraitSize.width + 10);
        self.messageContentView.frame = messageContentViewRect;
        
        self.bubbleBackgroundView.frame = CGRectMake(8, 0, bubbleBackgroundViewSize.width, bubbleBackgroundViewSize.height);
        UIImage *image = [RCKitUtility imageNamed:@"chat_to_bg_normal" ofBundle:@"RongCloud.bundle"];
        self.bubbleBackgroundView.image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(image.size.height * 0.8, image.size.width * 0.2, image.size.height * 0.2, image.size.width * 0.8)];
    }

    
    

    
    
}

+ (CGSize)getTextLabelSize:(LBDiscussBidMessage *)message {
    if ([message.content length] > 0) {
        float maxWidth = [UIScreen mainScreen].bounds.size.width -(10 + [RCIM sharedRCIM].globalMessagePortraitSize.width + 10) * 2 - 5 - 35;
       CGFloat height = [MDHTMLLabel sizeThatFitsHTMLString:message.content withFont:[UIFont systemFontOfSize:15] constraints:CGSizeMake(maxWidth, NSUIntegerMax) limitedToNumberOfLines:0];
        return CGSizeMake(maxWidth, height);
        
    } else {
        return CGSizeZero;
    }
}

+ (CGSize)getBubbleSize:(CGSize)textLabelSize {
    CGSize bubbleSize = CGSizeMake(textLabelSize.width, textLabelSize.height);
    
    if (bubbleSize.width + 12 + 20 > 50) {
        bubbleSize.width = bubbleSize.width + 12 + 20;
    } else {
        bubbleSize.width = 50;
    }
    if (bubbleSize.height + 5 + 5 > 35) {
        bubbleSize.height = bubbleSize.height + 5 + 5;
    } else {
        bubbleSize.height = 35;
    }
    
    return bubbleSize;
}

+ (CGSize)getBubbleBackgroundViewSize:(LBDiscussBidMessage *)message {
    CGSize textLabelSize = [[self class] getTextLabelSize:message];
    return [[self class] getBubbleSize:textLabelSize];
}

-(UIImageView *)headerView
{
    if (_headerView == nil) {
        
        _headerView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"lb_bid_tender_headicon"]];
        _headerView.frame = CGRectMake(9, 10, [RCIM sharedRCIM].globalMessagePortraitSize.width + 2, [RCIM sharedRCIM].globalMessagePortraitSize.height + 2);
        
    }
    return _headerView;
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
